<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/base.css" />
<link rel="stylesheet" href="css/util.css" />
<link rel="stylesheet" href="css/collect.css" />
<title>修改体验券</title>
</head>
<body>
<div class="content add_tyq">
	<div class="navigate_head clearfix">
		<p onclick="window.location.href='tyq_list.html'">体验劵列表</p>
		<p onclick="window.location.href='edit_tyq.html'" class="navi_current">修改体验劵</p>
		<!--<p onclick="window.location.href='tyq_appointment.html'">体验劵预约</p>-->
		<p onclick="window.location.href='tyq_type_list.html'">体验劵分类</p>
		<p onclick="window.location.href='add_tyq_type.html'">添加分类</p>
		<p class="navi_null"></p>
	</div>
	<div>
		<form class="add_role_form">
			<div class="public_input">
				<span>体验劵编号:</span>
				<input type="text" class="tyq_num" placeholder="体验劵编号"/>
			</div>
			<div class="public_input">
				<span>产品名称:</span>
				<select class="produce_name">
					<option class="0">请选择</option>
				</select>
			</div>
			<div class="public_input">
				<span>参与医院:</span>
				<select class="hos_name">
					<option class="0">请选择</option>
				</select>
			</div>
			<div class="public_input">
				<span>业务员名字:</span>
				<select class="salesman_name">
					<option class="0">请选择</option>
				</select>
			</div>
			<div class="public_input">
				<span>患者姓名:</span>
				<input type="text" class="patient_name" placeholder="开始时间"/>
			</div>
			<div class="public_input">
				<span>患者手机号:</span>
				<input type="text" class="mobile" placeholder="手机号"/>
			</div>
			<div class="public_input">
				<span>产品失效时间:</span>
				<input type="text" class="Wdate end_time" onfocus="WdatePicker({lang:'zh-cn',dateFmt: 'yyyy-MM-dd'})"/>
			</div>
			<div class="public_input confrim_btn">
				<span>.</span>
				<input type="button" class="confrim_add_tyq" value="修改"/>
			</div>
		</form>
	</div>
</div>
	
	
<script type="text/javascript" src="plug/jquery-1.11.3.js" ></script>
<script type="text/javascript" src="plug/template-web.js"></script>
<script type="text/javascript" src="js/toggle.js" ></script>
<script type="text/javascript" src="js/util.js"></script>
<script type="text/javascript" src="js/method.js"></script>
<!--日期插件-->
<link rel="stylesheet" href="plug/My97DatePicker/skin/WdatePicker.css" />
<script src="plug/My97DatePicker/WdatePicker.js"></script>
<!--医院列表-->
<script type="text/html" id="test_hos_list">
	{{each data value}}
		<option value="{{value.id}}">{{value.name}}</option>
	{{/each}}
</script>
<!--产品列表-->
<script type="text/html" id="test_produce_list">
	{{each data value}}
		<option value="{{value.id}}">{{value.name}}</option>
	{{/each}}
</script>
<!--业务员列表-->
<script type="text/html" id="test_salesman_list">
	{{each data value}}
		<option value="{{value.id}}">{{value.name}}</option>
	{{/each}}
</script>
<script>
$(function(){
	var hos_id ="";//医院id
	var produce_id ="";//产品id
	var salesman_id ="";//业务员id
	
	//初始化产品列表
	$.initSelect("/admin/experienceCoupon/getListBySelect.json",'test_produce_list',".produce_name");
	$(".produce_name").change(function(){
		produce_id = $(".produce_name").val();
		var data = {
			"experienceCouponId":produce_id
		}
		//初始化医院列表
		$.initSelect("/admin/experienceCoupon/getExperienceCouponHospital.json",'test_hos_list',".hos_name",data);
	});
	$(".hos_name").change(function(){
		hos_id = $(".hos_name").val();
	});
	//初始化业务员列表
	$.initSelect("/admin/salesman/getEnabledList.json",'test_salesman_list',".salesman_name");
	$(".salesman_name").change(function(){
		salesman_id = $(".salesman_name").val();
	});
	
	//初始化表单控件
	var data = {
		id:$.get_storage("tyq_id")
	}
	$.get_ajax("/admin/experienceReservation/getDetail.json",data,function(res){
		console.log(res);
		if(res.success){
			//初始化输入框
			$(".tyq_num").attr("value",res.data.sn);
			$(".patient_name").attr("value",res.data.username);
			$(".mobile").attr("value",res.data.mobile);//手机号
			$(".end_time").attr("value",res.data.invalidDate);
			//初始化医院选中的id
			hos_id = res.data.hospitalId;
			//定时器会让这里最后执行，延迟目的：先初始化科室列表，然后再初始化下拉选选中
			var setTime = setTimeout(function(){
				produce_id = res.data.experienceCouponId;
				$(".produce_name>[value="+produce_id+"]").attr("selected","selected");//选中
				var data = {
					"experienceCouponId":produce_id
				}
				//初始化医院列表
				$.get_ajax("/admin/experienceCoupon/getExperienceCouponHospital.json",data,function(res){
					console.log(res);
					if(res.success){
						$(".hos_name").html("<option selected='selected' value='0'>请选择</option>");
						var html = template('test_hos_list',res);
						$(".hos_name").append(html);
						console.log(hos_id);
						$(".hos_name>[value="+hos_id+"]").attr("selected","selected");
					}else{
						console.log(res.data);
					}
				});
				salesman_id = res.data.salesmanId;
				$(".salesman_name>[value="+salesman_id+"]").attr("selected","selected");
			},1000);
		}
	});
	
	$(".confrim_add_tyq").click(function(){
		var tyq_num = $(".tyq_num").val();
		var patient_name = $(".patient_name").val();
		var mobile = $(".mobile").val();
		var end_time = $(".end_time").val();
		
		var data = {
			"id":$.get_storage("tyq_id"),
			"sn":tyq_num,
			"hospitalId":hos_id,
			"experienceCouponId":produce_id,
			"salesmanId":salesman_id,
			"username":patient_name,
			"mobile":mobile,
			"invalidDate":end_time
		}
		//验证
		if(!$.verify(tyq_num,"体验券编号")){return false;}
		if(!$.verify(patient_name,"患者姓名")){return false;}
		if(!$.verify(end_time,"产品失效时间")){return false;}
		if(!$.verifyPhone(mobile,"手机号")){return false;}
		if(!$.verifyNum(hos_id,"参与医院")){return false;}
		if(!$.verifyNum(produce_id,"产品名称")){return false;}
		if(!$.verifyNum(salesman_id,"业务员名字")){return false;}
		$.post_ajax("/admin/experienceReservation/update",data,function(data){
			if(data.success==true){
				alert("修改成功");
				window.location.href = "tyq_list.html";
			}else{
				alert(data.data);
			}
		});
	});
});
</script>
</body>
</html>
